package com.zengde.chapter1.main;

import com.mysql.cj.jdbc.MysqlDataSource;
import com.zengde.chapter1.conditional.DatabaseConditional;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Controller
@SpringBootApplication
//@ComponentScan(value = "com.zengde.chapter1.*",lazyInit = false)
@ComponentScan(basePackages = "com.zengde.chapter1.*",excludeFilters = @ComponentScan.Filter(type= FilterType.ANNOTATION, classes = Service.class))
// 指定加载的配置文件，并设置为如果找不到文件则忽略，不会报
//@PropertySource(value={"classpath:jdbc.properties"}, ignoreResourceNotFound=false)
// 启用属性文件的配置机制
//@EnableConfigurationProperties
public class Chapter1Application {


    @RequestMapping("/test")
    @ResponseBody
    public Map<String, String> test() {
        var map = new HashMap<String, String>();
        map.put("key", "value");
        return map;
    }

    // 请求路径


   @GetMapping("/test2")
   public String test2(HttpServletRequest request) {
      // 设置请求属性
      request.setAttribute("name", "张三");
      // 返回“test”，这样会映射到Thymeleaf模板（test.html）上，就可以渲染页面了
      return "test";
   }



    public static void main(String[] args) {
        SpringApplication.run(Chapter1Application.class, args);
    }


//    @Bean(name = "dataSource")
//    @Conditional(DatabaseConditional.class)
//    public DataSource getDataSource(
//            @Value("${database.url}") String url,
//            @Value("${database.username}") String username,
//            @Value("${database.password}") String password
//    ) {
//        System.out.println("初始化数据源");
//        var dataSource = new MysqlDataSource();
//        dataSource.setUrl(url);
//        dataSource.setUser(username);
//        dataSource.setPassword(password);
//        return dataSource;
//    }

}
